import { KnowledgeRouteKey } from '@/constants/knowledge';
import { IKnowledge } from '@/interfaces/database/knowledge';
import { formatDate } from '@/utils/date';
import {
  CalendarOutlined,
  FileTextOutlined,
  MailOutlined,
  UserOutlined,
} from '@ant-design/icons';
import { Avatar, Card, Space, Typography } from 'antd';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'umi';

import OperateDropdown from '@/components/operate-dropdown';
import { useDeleteKnowledge } from '@/hooks/knowledge-hooks';
import authorizationUtil from '@/utils/authorization-util';
import styles from './index.less';

const { Text } = Typography;

interface IProps {
  item: IKnowledge;
}

const KnowledgeCard = ({ item }: IProps) => {
  // 从 authorizationUtil 中获取用户信息，检查返回值是否为 null
  const userInfoStr = authorizationUtil.getUserInfo();
  const userInfo = userInfoStr ? JSON.parse(userInfoStr) : {}; // 如果 userInfoStr 为 null，则赋予默认空对象

  const navigate = useNavigate();
  const { t } = useTranslation();
  const { deleteKnowledge } = useDeleteKnowledge();

  const removeKnowledge = async () => {
    return deleteKnowledge(item.id);
  };

  const handleCardClick = () => {
    navigate(`/knowledge/${KnowledgeRouteKey.Dataset}?id=${item.id}`, {
      state: { from: 'list' },
    });
  };
  return (
    <Card className={styles.card} onClick={handleCardClick}>
      <div className={styles.container}>
        <div className={styles.content}>
          <Avatar size={40} icon={<UserOutlined />} src={item.avatar} />
          {userInfo.email == item.created_by_email && (
            <OperateDropdown deleteItem={removeKnowledge} />
          )}
        </div>
        <div className={styles.titleWrapper}>
          <span className={styles.title}>{item.name}</span>
          <span className={styles.description}>{item.description}</span>
        </div>
        {/* 新增的展示信息 */}
        <div className={styles.creatorInfo}>
          <Space direction="vertical" size={0}>
            <div>
              <UserOutlined className={styles.leftIcon} />
              <Text className={styles.rightText}>
                {t('knowledgeList.creatorName')}: {item.created_by_nickname}
              </Text>
            </div>
            <div>
              <MailOutlined className={styles.leftIcon} />
              <Text className={styles.rightText}>
                {t('knowledgeList.creatorEmail')}: {item.created_by_email}
              </Text>
            </div>
          </Space>
        </div>
        <div className={styles.footer}>
          <div className={styles.footerTop}>
            <div className={styles.bottomLeft}>
              <FileTextOutlined className={styles.leftIcon} />
              <span className={styles.rightText}>
                <Space>
                  {item.doc_num}
                  {t('knowledgeList.doc')}
                </Space>
              </span>
            </div>
          </div>
          <div className={styles.bottom}>
            <div className={styles.bottomLeft}>
              <CalendarOutlined className={styles.leftIcon} />
              <span className={styles.rightText}>
                {formatDate(item.update_time)}
              </span>
            </div>
          </div>
        </div>
      </div>
    </Card>
  );
};

export default KnowledgeCard;
